package com.gdssoft.pdaweb.dao.impl;

/*
 *  捷达世软件(深圳)有限公司
 *  Copyright (C) 2008-2010 ShenZhen JieDaShi Software Limited Company.
 */

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.gdssoft.core.dao.impl.BaseDaoImpl;
import com.gdssoft.core.util.DateUtil;
import com.gdssoft.pdaweb.dao.RepairWorkOrderDao;
import com.gdssoft.pdaweb.model.RepairWorkOrder;

@SuppressWarnings("unchecked")
public class RepairWorkOrderDaoImpl extends BaseDaoImpl<RepairWorkOrder> 
implements RepairWorkOrderDao {

	public RepairWorkOrderDaoImpl() {
		super(RepairWorkOrder.class);
	}
	
	/**
	 * 获取人员维修任务信息
	 * @param dutyPerson:维修人
	 * @param assignmentid:工单唯一标识
	 * @param repairStatus:维修状态
	 * @param assetNo:
	 * @param createDateFrom:报修日期开始
	 * @param createDatetTo:报修日期结束
	 * @return
	 * @throws Exception 
	 */
	public List<RepairWorkOrder> listRepairWorkOrderByDutyPerson(String dutyPerson,String assignmentid,
			String repairStatus,String assetNo,String completionDateFrom,String completionDateTo){
		String querySql = " from RepairWorkOrder rwo where 1=1";
		List<Object> paraList = new ArrayList<Object>();
		if(StringUtils.isNotBlank(dutyPerson)){
			querySql += " and rwo.appointPeople=?";
			paraList.add(dutyPerson);
		}
		if(StringUtils.isNotBlank(assignmentid)){
			//querySql += " and rwo.woNum=?";
			querySql += " and rwo.assignmentId=?";
			long assignmentId = Long.parseLong(assignmentid);
			paraList.add(assignmentId);
		}
		if(StringUtils.isNotBlank(repairStatus)){
			querySql += " and rwo.status=?";
			paraList.add(repairStatus);
		}
		if(StringUtils.isNotBlank(assetNo)){
			querySql += " and rwo.assetNo=?";
			paraList.add(assetNo);
		}
		if(StringUtils.isNotBlank(completionDateFrom)){
			//try {
				querySql += " and rwo.createdDt>=?";
				paraList.add(DateUtil.dateParse(completionDateFrom,"yyyy-MM-dd"));
			/*} catch (ParseException e) {
				logger.info("輸入日期:" + completionDateFrom +",格式轉換錯誤");
			}*/
		}
		if(StringUtils.isNotBlank(completionDateTo)){
			//try {
				querySql += " and rwo.createdDt<=?";
				paraList.add(DateUtil.dateParse(completionDateTo,"yyyy-MM-dd"));
				/*}
			catch (ParseException e) {
				logger.info("輸入日期:" + completionDateTo +",格式轉換錯誤");
			}*/
		}
		
		return this.findByHql(querySql, paraList.toArray());
	}
	public List<RepairWorkOrder> listUnSynchroRepairWorkOrder(){
		
		String hql="from RepairWorkOrder where feedbackDt is null";
		return this.findByHql(hql);
		
	}
}

